package org.elec.mac.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.elec.mac.entity.ExecuteResult;
import org.elec.mac.entity.SDevice;
import org.elec.mac.entity.SDevice;
import org.elec.mac.page.PageList;
import org.elec.mac.page.PageProperty;
import org.elec.mac.service.SDeviceService;
import org.elec.mac.tag.UserNameTag;
import org.elec.mac.utils.JSONUtil;
import org.elec.mac.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
public class DeviceController extends BaseController {

    @Autowired
    private SDeviceService sDeviceService;

    @RequestMapping(value = "/device_list.do")
    public String deviceListHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model,
                                    @RequestParam(value = "action", required = false, defaultValue = "") String action,
                                    @RequestParam(value = "limit", required = false) String limit,
                                    @RequestParam(value = "page", required = false) String page) throws IOException {
        if("page".equals(action)) {
            PageProperty pp = new PageProperty();
            Map<String, Object> map = new HashMap<String, Object>();
            try {
                this.setPageInfo(request.getCookies(), pp, limit, page);
                PageList<SDevice> lintPageList = sDeviceService.getPageList(pp);
                map.put("success", true);
                map.put("records", lintPageList.getRecords());
                map.put("recordsTotal", lintPageList.getTotalRecords());
                map.put("recordsFiltered", lintPageList.getTotalRecords());
            } catch(Exception ex) {
                log.error("设备列表获取错误", ex);
                map.put("success", false);
                map.put("msg", "设备列表获取错误");
            }
            response.getWriter().write(JSONUtil.map2json(map));
            return null;
        }
        return "/device/device_list";
    }

    @RequestMapping(value = "/device_add.do")
    public void deviceAddHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model,
                                 @RequestParam(value = "action", required = false, defaultValue = "") String action) throws IOException {
        ExecuteResult result = new ExecuteResult();
        result.setSuccess(true);
        result.setMsg("成功");
        if("do".equals(action)) {
            try {
                String name = this.getParameter(request, "name");
                Integer leng = Utils.parseInt(this.getParameter(request, "leng"), 0);
                Integer wid = Utils.parseInt(this.getParameter(request, "wid"), 0);
                Integer hig = Utils.parseInt(this.getParameter(request, "hig"), 0);
                String modeln = this.getParameter(request, "modeln");
                Integer voltage = Utils.parseInt(this.getParameter(request, "voltage"), 0);
                String commmodel = this.getParameter(request, "commmodel");
                Boolean advscree = Utils.parseBoolean(this.getParameter(request, "advscree"), true);
                String asupplier = this.getParameter(request, "asupplier");
                Boolean cscreen = Utils.parseBoolean(this.getParameter(request, "cscreen"), true);

                Map<String, Object> param = new HashMap<>();
                param.put("name", name);
                SDevice device = sDeviceService.get(param);

                if(StringUtils.isBlank(name)) {
                    result.setSuccess(false);
                    result.setMsg("设备名不能为空");
                }
                if(leng == 0 || wid == 0 || hig == 0) {
                    result.setSuccess(false);
                    result.setMsg("设备规格不能为空");
                }
                if(StringUtils.isBlank(modeln)) {
                    result.setSuccess(false);
                    result.setMsg("设备型号不能为空");
                }
                if(voltage == 0) {
                    result.setSuccess(false);
                    result.setMsg("设备电压不能为空");
                }
                if(null != device) {
                    result.setSuccess(false);
                    result.setMsg("设备名已存在");
                }
                if(result.isSuccess()) {
                    SDevice sDevice = new SDevice();
                    sDevice.setName(name);
                    sDevice.setLeng(leng);
                    sDevice.setWid(wid);
                    sDevice.setHig(hig);
                    sDevice.setModeln(modeln);
                    sDevice.setVoltage(voltage);
                    sDevice.setCommmodel(commmodel);
                    sDevice.setAdvscree(advscree);
                    sDevice.setAsupplier(asupplier);
                    sDevice.setCscreen(cscreen);
                    sDevice.setCreateTime(new Date());
                    sDevice.setCreator(UserNameTag.getUserName());
                    sDevice.setModifyTime(new Date());
                    sDevice.setModifier(UserNameTag.getUserName());
                    sDevice.setFlag(true);
                    sDeviceService.insert(sDevice);
                    log.info("设备新增");
                }
            } catch(Exception ex) {
                log.error("设备新增错误", ex);
                result.setSuccess(false);
                result.setMsg("设备新增错误");
            }
        }
        log.info(JSONUtil.object2json(result));
        response.getWriter().write(JSONUtil.object2json(result));
    }

    @RequestMapping(value = "/device_edit.do")
    public void deviceEditHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model,
                                  @RequestParam(value = "action", required = false, defaultValue = "") String action,
                                  @RequestParam(value = "id", required = false, defaultValue = "") String id) throws IOException {
        ExecuteResult result = new ExecuteResult();
        result.setSuccess(true);
        result.setMsg("成功");
        if("edit".equals(action)) {
            try {
                Map<String, Object> params = new HashMap<>();
                params.put("id", Utils.parseLong(id, 0L));
                SDevice sDevice = sDeviceService.get(params);
                result.getBody().put("sDevice", sDevice);
                log.info("设备修改开启");
            } catch(Exception ex) {
                log.error("设备修改开启错误", ex);
                result.setSuccess(false);
                result.setMsg("设备修改开启错误");
            }
        }
        if("doEdit".equals(action)) {
            try {
                String name = this.getParameter(request, "name");
                Integer leng = Utils.parseInt(this.getParameter(request, "leng"), 0);
                Integer wid = Utils.parseInt(this.getParameter(request, "wid"), 0);
                Integer hig = Utils.parseInt(this.getParameter(request, "hig"), 0);
                String modeln = this.getParameter(request, "modeln");
                Integer voltage = Utils.parseInt(this.getParameter(request, "voltage"), 0);
                String commmodel = this.getParameter(request, "commmodel");
                Boolean advscree = Utils.parseBoolean(this.getParameter(request, "advscree"), true);
                String asupplier = this.getParameter(request, "asupplier");
                Boolean cscreen = Utils.parseBoolean(this.getParameter(request, "cscreen"), true);
                if(StringUtils.isBlank(name)) {
                    result.setSuccess(false);
                    result.setMsg("设备名不能为空");
                }
                if(leng == 0 || wid == 0 || hig == 0) {
                    result.setSuccess(false);
                    result.setMsg("设备规格不能为空");
                }
                if(StringUtils.isBlank(modeln)) {
                    result.setSuccess(false);
                    result.setMsg("设备型号不能为空");
                }
                if(voltage == 0) {
                    result.setSuccess(false);
                    result.setMsg("设备电压不能为空");
                }
                if(result.isSuccess()) {
                    SDevice sDevice = new SDevice();
                    sDevice.setId(Utils.parseLong(id, 0L));
                    sDevice.setName(name);
                    sDevice.setLeng(leng);
                    sDevice.setWid(wid);
                    sDevice.setHig(hig);
                    sDevice.setModeln(modeln);
                    sDevice.setVoltage(voltage);
                    sDevice.setCommmodel(commmodel);
                    sDevice.setAdvscree(advscree);
                    sDevice.setAsupplier(asupplier);
                    sDevice.setCscreen(cscreen);
                    sDevice.setModifyTime(new Date());
                    sDevice.setModifier(UserNameTag.getUserName());
                    sDeviceService.update(sDevice);
                    log.info("设备修改");
                }
            } catch(Exception ex) {
                log.error("设备修改错误", ex);
                result.setSuccess(false);
                result.setMsg("设备修改错误");
            }
        }
        if("doDelete".equals(action)) {
            try {
                SDevice sDevice = new SDevice();
                sDevice.setId(Utils.parseLong(id, 0L));
                sDevice.setModifyTime(new Date());
                sDevice.setModifier(UserNameTag.getUserName());
                sDevice.setFlag(false);
                sDeviceService.update(sDevice);
                log.info("设备删除");
            } catch(Exception ex) {
                log.error("设备删除错误", ex);
                result.setSuccess(false);
                result.setMsg("设备删除错误");
            }
        }
        log.info(JSONUtil.object2json(result));
        response.getWriter().write(JSONUtil.object2json(result));
    }
}
